package com.cy.interfacetest.activity;

import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import com.cy.interfacetest.R;
import com.cy.interfacetest.entity.AppInfo;

import java.io.File;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


/**
 * 添加
 */
@RequiresApi(api = Build.VERSION_CODES.N)
public class BaseActivity extends AppCompatActivity {

    private static final String TAG = "BaseActivity";

    protected File file;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base);
        file = new File(this.getCacheDir(), "network.log");
    }


    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.policy_config:
                Log.i(TAG, "onOptionsItemSelected: " + ConfigPolicyActivity.class.getSimpleName());
                startActivity(new Intent(this, ConfigPolicyActivity.class));
                break;
            case R.id.device_control:
                Log.i(TAG, "onOptionsItemSelected: " + DeviceControlActivity.class.getSimpleName());
                startActivity(new Intent(this, DeviceControlActivity.class));
                break;
            case R.id.throttling_policy:
                Log.i(TAG, "onOptionsItemSelected: " + MainActivity.class.getSimpleName());
                startActivity(new Intent(this, MainActivity.class));
                break;
            case R.id.device_info:
                Log.i(TAG, "onOptionsItemSelected: " + DeviceInfoActivity.class.getSimpleName());
                startActivity(new Intent(this, DeviceInfoActivity.class));
                break;
            case R.id.device_owner:
                Log.i(TAG, "onOptionsItemSelected: " + DeviceOwnerActivity.class.getSimpleName());
                startActivity(new Intent(this, DeviceOwnerActivity.class));
                break;

        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;
    }


    public static void switchPassOrFailure(boolean result, View view) {

        Log.i(TAG, "switchPassOrFailure: " + (result ? "接口调用成功" : "接口调用失败"));
        if (Objects.isNull(view)) {
            throw new RuntimeException("view 为空!");
        }
        if (result) {
            view.setBackgroundTintList(ColorStateList.valueOf(Color.GREEN));
        } else {
            view.setBackgroundTintList(ColorStateList.valueOf(Color.RED));
        }
    }


    public static boolean isZETDevice() {
        return "ZTE A2022H".equalsIgnoreCase(Build.MODEL) || "ZTE A2122H".equalsIgnoreCase(Build.MODEL) || "ZTE A2021H".equalsIgnoreCase(Build.MODEL) || "ZTE A2023BH".equalsIgnoreCase(Build.MODEL)   //A41ultra pro
                || "ZTE A2023H".equalsIgnoreCase(Build.MODEL)    //A41ultra
                || "ZTE A2023P".equalsIgnoreCase(Build.MODEL) || "ZTE A2022P".equalsIgnoreCase(Build.MODEL) || "ZTE 9043N".equalsIgnoreCase(Build.MODEL) // ZTE A2023;
                || "ZTE A2023".equalsIgnoreCase(Build.MODEL);// ZTE A2023;

    }


    public List<AppInfo> getInstallApps() {
        PackageManager packageManager = getPackageManager();
        List<ApplicationInfo> packages = packageManager.getInstalledApplications(PackageManager.GET_META_DATA);
        return packages.stream().filter(e -> (e.flags & ApplicationInfo.FLAG_SYSTEM) == 0).map(e -> {
            String appName = packageManager.getApplicationLabel(e).toString();
            Log.d("AppInfo", "App Name: " + appName + ", Device ID: " + e.packageName);
            return new AppInfo(appName, e.packageName);
        }).collect(Collectors.toList());
    }
}